package DAL;

import DatabaseAccess.ConvertDateToString;
import Entities.TblExamination;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class ExaminationDAL {

    private ResultSet objResult;
    public List<TblExamination> list;

    public ExaminationDAL() {
    }

    public boolean add(TblExamination examination) {
        ConvertDateToString convert = new ConvertDateToString();
        String examDate = convert.convertDateToMMddyyyy(examination.getExamDate());
        String EndDateToRequest = convert.convertDateToMMddyyyy(examination.getEndDateToRequest());
        
        int result = 0;
        String[] params = new String[6];
        params[0] = examination.getExamName();
        params[1] = String.valueOf(examination.getExamType());
        params[2] = String.valueOf(examination.getSubjectId());
        params[3] = examDate;
        params[4] = EndDateToRequest;
        params[5] = String.valueOf(examination.getPriceToRequest());

        try {
            result = SQLHelper.executeUpdate("ProcInsertExamination", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblExamination examination) {        
        ConvertDateToString convert = new ConvertDateToString();
        String examDate = convert.convertDateToMMddyyyy(examination.getExamDate());
        String EndDateToRequest = convert.convertDateToMMddyyyy(examination.getEndDateToRequest());

        int result = 0;
        String[] params = new String[7];
        params[0] = String.valueOf(examination.getExamId());
        params[1] = examination.getExamName();
        params[2] = String.valueOf(examination.getExamType());
        params[3] = String.valueOf(examination.getSubjectId());
        params[4] = examDate;
        params[5] = EndDateToRequest;
        params[6] = String.valueOf(examination.getPriceToRequest());

        try {
            result = SQLHelper.executeUpdate("procUpdateExamination", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblExamination examination) {
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(examination.getSubjectId());

        try {
            result = SQLHelper.executeUpdate("procDeleteExamination", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblExamination> getAll() {
        list = new ArrayList<TblExamination>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllExamination", null);
            while (objResult.next()) {
                TblExamination examination = new TblExamination();
                examination.setExamId(objResult.getInt("SubjectId"));
                examination.setExamName(objResult.getString("ExamName"));
                examination.setExamType(objResult.getInt("ExamType"));
                examination.setSubjectId(objResult.getInt("SubjectId"));
                examination.setExamDate(objResult.getDate("ExamDate"));
                examination.setEndDateToRequest(objResult.getDate("EndDateToRequest"));
                examination.setPriceToRequest(objResult.getFloat("PriceToRequest"));

                list.add(examination);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblExamination getById(int id) {
        TblExamination examination = new TblExamination();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetExaminationById", params);
            while (objResult.next()) {                
                examination.setExamId(objResult.getInt("SubjectId"));
                examination.setExamName(objResult.getString("ExamName"));
                examination.setExamType(objResult.getInt("ExamType"));
                examination.setSubjectId(objResult.getInt("SubjectId"));
                examination.setExamDate(objResult.getDate("ExamDate"));
                examination.setEndDateToRequest(objResult.getDate("EndDateToRequest"));
                examination.setPriceToRequest(objResult.getFloat("PriceToRequest"));
            }

            return examination;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public List<TblExamination> getBySubjectId(int subjectId) {
        list = new ArrayList<TblExamination>();
        String[] params = new String[1];
        params[0] = String.valueOf(subjectId);
        
        try {
            objResult = SQLHelper.executeQuery("procGetExaminationBySubjectId", params);
            while (objResult.next()) {
                TblExamination examination = new TblExamination();
                examination.setExamId(objResult.getInt("SubjectId"));
                examination.setExamName(objResult.getString("ExamName"));
                examination.setExamType(objResult.getInt("ExamType"));
                examination.setSubjectId(objResult.getInt("SubjectId"));
                examination.setExamDate(objResult.getDate("ExamDate"));
                examination.setEndDateToRequest(objResult.getDate("EndDateToRequest"));
                examination.setPriceToRequest(objResult.getFloat("PriceToRequest"));

                list.add(examination);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
